package com.scau.yangsuiyu.estore.controller.seller.manageBasicInfo;

import com.scau.yangsuiyu.estore.dababases.dao.SellerDao;
import com.scau.yangsuiyu.estore.dababases.entity.Seller;
import com.scau.yangsuiyu.estore.util.FormValidate;
import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 卖家安全设置界面Controller
 * @date 2019-06-07 19:43
 * @email suiyu_yang@163.com
 */
@FXMLController
public class InfoSecuritySeller extends AnchorPane implements Initializable {
    private Seller seller;

    @FXML
    private Button modifyEmailButton;

    @FXML
    private Button modifyPhoneButton;
    @FXML
    private TextField emailTF;

    @FXML
    private TextField phoneTF;

    @FXML
    private PasswordField originalPswPF;

    @FXML
    private PasswordField currentPswPF;

    @FXML
    private Label emailErrorLabel;

    @FXML
    private Label phoneErrorLabel;

    private BooleanProperty ifModifyEmail = new SimpleBooleanProperty(false);
    private BooleanProperty ifModifyPhone = new SimpleBooleanProperty(false);
    private FormValidate<TextField> emailRegValidate = new FormValidate<>();
    private FormValidate<TextField> phoneRegValidate = new FormValidate<>();

    public InfoSecuritySeller(){

    }
    public InfoSecuritySeller(Seller seller) {
        this.seller = seller;
        loadFXML();
        initUI();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/seller/manageBasicInfo/InfoSecuritySeller.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        TextField[] mobileNoTFs = {phoneTF};
        Label[] regularLabels = {phoneErrorLabel};
        phoneErrorLabel.setText("手机号错误！");
        phoneRegValidate.installRegularValidation(mobileNoTFs, FormValidate.CHINAMOBILENO_REG, regularLabels);

        TextField[] emailTFs = {emailTF};
        Label[] emailLabels = {emailErrorLabel};
        emailErrorLabel.setText("电子邮箱错误！");
        emailRegValidate.installRegularValidation(emailTFs, FormValidate.EMAIL_REG, emailLabels);

        emailTF.editableProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (newValue) {
                    emailTF.setStyle("-fx-border-color:#b6d6bd;");
                } else {
                    emailTF.setStyle("-fx-background-color:transparent;");
                }
            }
        });
        phoneTF.editableProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (newValue) {
                    phoneTF.setStyle("-fx-border-color:#b6d6bd;");
                } else {
                    phoneTF.setStyle("-fx-background-color:transparent;");
                }
            }
        });

    }

    private void initUI() {
        ifModifyEmail.setValue(false);
        emailTF.editableProperty().bind(ifModifyEmail);
        phoneTF.editableProperty().bind(ifModifyPhone);

        if (seller.getEmail() != null) {
            //已有邮箱，不能修改
            ifModifyEmail.setValue(false);

            emailTF.setText(seller.getEmail());


        } else { //为空
            ifModifyEmail.setValue(true);
            emailTF.setPromptText("请填写您的电子邮箱");

        }

        if (seller.getPhone() != null) {
            ifModifyPhone.setValue(false);

            phoneTF.setText(seller.getPhone());

        }
    }

    @FXML
    private void modifyEmailClick() {
        if (ifModifyEmail.getValue()) {
            modifyEmail();
        } else {
            ifModifyEmail.setValue(true);
        }
    }

    @FXML
    private void modifyPhoneClick() {
        if (ifModifyPhone.getValue()) {
            modifyPhone();
        } else {
            ifModifyPhone.setValue(true);
        }
    }

    @FXML
    private void modifyPasswordClick() {
        String password = originalPswPF.getText();

        if (password.equals(seller.getPsw())) {
            modifyPassword();
        } else {
            new Alert(Alert.AlertType.ERROR, "密码错误！").showAndWait();
        }
    }

    private void modifyPhone() {
        String phone = phoneTF.getText();

        if (phoneRegValidate.isValid()) {
            if (!phone.equals("")) {
                if (!phone.equals(seller.getPhone())) {
                    if (new SellerDao().getSellerByPhone(phone) == null) {
                        seller.setPhone(phone);
                        new SellerDao().update(seller);
                        ifModifyPhone.setValue(false);
                    } else {
                        new Alert(Alert.AlertType.ERROR, "手机号码已存在！").showAndWait();
                        phoneTF.setText(seller.getPhone());
                    }
                }
                ifModifyPhone.setValue(false);
            } else {
                new Alert(Alert.AlertType.ERROR, "请输入手机号码！").showAndWait();
                phoneTF.setText(seller.getPhone());
            }
        }

    }

    private void modifyEmail() {
        System.out.println("修改邮箱");
        String email = emailTF.getText();

        if (emailRegValidate.isValid()) {
            if (!email.equals("")) {
                if (!email.equals(seller.getEmail())) {
                    if (new SellerDao().getSellerByEmail(email) == null) {
                        seller.setEmail(email);
                        new SellerDao().update(seller);
                        ifModifyEmail.setValue(false);
                    } else {
                        new Alert(Alert.AlertType.ERROR, "该邮箱已被其他用户绑定！").showAndWait();
                        emailTF.setText(seller.getEmail());
                    }
                }
                ifModifyEmail.setValue(false);
            } else {
                new Alert(Alert.AlertType.ERROR, "请输入邮箱！").showAndWait();
                emailTF.setText(seller.getEmail());
            }
        }
    }

    private void modifyPassword() {
        String password = currentPswPF.getText();

        if (!password.equals("")) {
            seller.setPsw(password);
            new SellerDao().update(seller);

            ifModifyPhone.setValue(false);
            originalPswPF.setText("");
            currentPswPF.setText("");
            new Alert(Alert.AlertType.INFORMATION, "修改密码成功！").showAndWait();
        } else {
            new Alert(Alert.AlertType.ERROR, "修改密码为空！").showAndWait();
        }
    }
}
